package com.wcx.imitationwechatbilllist;

import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author: 魏
 * @date: 2025/9/29
 * 自定义StickyHeaderDecoration类
 */
public class StickyHeaderDecoration extends RecyclerView.ItemDecoration {
    public StickyHeaderInterface listener;
    private View currentStickyView;
    private int currentStickyPosition = RecyclerView.NO_POSITION;

    // 添加这两个方法，监听头部点击事件
    public View getCurrentStickyView() {
        return currentStickyView;
    }

    public int getCurrentStickyPosition() {
        return currentStickyPosition;
    }

    public StickyHeaderDecoration(StickyHeaderInterface listener) {
        this.listener = listener;
    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent,
                           @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);

        View topChild = parent.getChildAt(0);
        if (topChild == null) return;

        int topChildPosition = parent.getChildAdapterPosition(topChild);
        if (topChildPosition == RecyclerView.NO_POSITION) return;

        int headerPosition = listener.getHeaderPositionForItem(topChildPosition);
        if (headerPosition == RecyclerView.NO_POSITION) return;

        if (currentStickyPosition != headerPosition || currentStickyView == null) {
            currentStickyView = listener.getHeaderView(parent, headerPosition);
            measureHeaderView(currentStickyView, parent);
            currentStickyPosition = headerPosition;
            // 设置头部点击事件
            currentStickyView.setOnClickListener(v -> {
                listener.onHeaderClick(headerPosition);
            });
        }

        int nextHeaderPosition = findNextHeaderPosition(parent, topChildPosition);
        int translateY = 0;

        if (nextHeaderPosition != RecyclerView.NO_POSITION) {
            View nextHeader = parent.getChildAt(nextHeaderPosition - topChildPosition);
            if (nextHeader != null) {
                int bottom = currentStickyView.getBottom();
                int nextHeaderTop = nextHeader.getTop();
                if (nextHeaderTop < bottom) {
                    translateY = nextHeaderTop - bottom;
                }
            }
        }

        c.save();
        c.translate(0, translateY);
        currentStickyView.draw(c);
        c.restore();
    }

    private int findNextHeaderPosition(RecyclerView parent, int position) {
        int itemCount = parent.getAdapter().getItemCount();
        for (int i = position + 1; i < itemCount; i++) {
            if (listener.isHeader(i)) {
                return i;
            }
        }
        return RecyclerView.NO_POSITION;
    }

    private void measureHeaderView(View headerView, ViewGroup parent) {
        int widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY);
        int heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight(), View.MeasureSpec.UNSPECIFIED);

        headerView.measure(widthSpec, heightSpec);
        headerView.layout(0, 0, headerView.getMeasuredWidth(), headerView.getMeasuredHeight());
    }

}
